package com.tms.app.tms.utils;

import android.net.Uri;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.LubanOptions;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;


/**
 * - 支持通过相机拍照获取图片
 * - 支持从相册选择图片
 * - 支持从文件选择图片
 * - 支持多图选择
 * - 支持批量图片裁切
 * - 支持批量图片压缩
 * - 支持对图片进行压缩
 * - 支持对图片进行裁剪
 * - 支持对裁剪及压缩参数自定义
 * - 提供自带裁剪工具(可选)
 * - 支持智能选取及裁剪异常处理
 * - 支持因拍照Activity被回收后的自动恢复
 * Author: crazycodeboy
 * Date: 2016/9/21 0007 20:10
 * Version:4.0.0
 * 技术博文：http://www.cboy.me
 * GitHub:https://github.com/crazycodeboy
 * Eamil:crazycodeboy@gmail.com
 */
public class PhotoHelper {


    /*******
     * 调用示例
     * @param takePhoto
     */
    private   void onClick(TakePhoto takePhoto){
        File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists())file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);

        configCompress(takePhoto);
        configTakePhotoOption(takePhoto);
        int limit=5;
        takePhoto.onPickMultipleWithCrop(limit,getCropOptions());// 设置选取数量，多选带剪裁，单选不用设置
        takePhoto.onPickMultiple(limit);//

        takePhoto.onPickFromGalleryWithCrop(imageUri,getCropOptions());// 从相册选取
        takePhoto.onPickFromGallery();

        takePhoto.onPickFromDocumentsWithCrop(imageUri,getCropOptions());//从文件选择
        takePhoto.onPickFromDocuments();

        takePhoto.onPickFromCaptureWithCrop(imageUri,getCropOptions());// 拍照带剪裁
        takePhoto.onPickFromCapture(imageUri);
    }
    public static void pickPhoto(TakePhoto takePhoto){
        pickPhoto(takePhoto,true);
    }
    public static void pickPhoto(TakePhoto takePhoto,boolean iscrop){
        pickPhoto(takePhoto,iscrop,true);
    }
    public static void pickPhoto(TakePhoto takePhoto,boolean iscrop, boolean iscompress){
        configCompress(takePhoto,iscompress);
        configTakePhotoOption(takePhoto);
        File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists())file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);
        if (iscrop)
            takePhoto.onPickFromGalleryWithCrop(imageUri,getCropOptions());// 从相册选取
        else
            takePhoto.onPickFromGallery();
    }

    public static void takePhoto(TakePhoto takePhoto){
        takePhoto(takePhoto,true);
    }
    public static void takePhoto(TakePhoto takePhoto,boolean iscrop){
        takePhoto(takePhoto,iscrop,true);
    }
    public static void takePhoto(TakePhoto takePhoto,boolean iscrop,boolean iscompress){
        configCompress(takePhoto,iscompress);
        configTakePhotoOption(takePhoto);
        File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);
        //Uri imageUri= FileProvider.getUriForFile(activity,"me.xifengwanzhao.fileprovider", file);//这里进行替换uri的获得方式
        if (iscrop)
            takePhoto.onPickFromCaptureWithCrop(imageUri,getCropOptions());// 拍照带剪裁
        else
            takePhoto.onPickFromCapture(imageUri);
    }

    private static void configTakePhotoOption(TakePhoto takePhoto){
            TakePhotoOptions.Builder builder=new TakePhotoOptions.Builder();
            builder.setWithOwnGallery(false);//使用TakePhoto自带相册
            builder.setCorrectImage(true);//纠正拍照的照片旋转角度
            takePhoto.setTakePhotoOptions(builder.create());
    }

    /****
     * 设置压缩选项
     * @param takePhoto
     */
    private static void configCompress(TakePhoto takePhoto){
        configCompress(takePhoto,true);
    }
    private static void configCompress(TakePhoto takePhoto,boolean saveReal){
        configCompress(takePhoto,true,saveReal);
    }
    private static void configCompress(TakePhoto takePhoto,boolean compress,boolean saveReal){

        int maxSize= 102400;//大小不超过B
        int etCropWidth= 856;// 宽度
        int etHeightPx= 580;
        boolean showProgressBar=true;//显示压缩进度条
        boolean enableRawFile = saveReal;//拍照压缩后是否保存原图
        CompressConfig config;
       // 使用自带压缩工具
            config=new CompressConfig.Builder()
                    .setMaxSize(maxSize)
                    .setMaxPixel(etCropWidth>=etHeightPx? etCropWidth:etHeightPx)
                    .enableReserveRaw(enableRawFile)
                    .create();
        //Luban压缩工具
//            LubanOptions option=new LubanOptions.Builder()
//                    .setMaxHeight(etHeightPx)
//                    .setMaxWidth(etCropWidth)
//                    .setMaxSize(maxSize)
//                    .create();
//            config= CompressConfig.ofLuban(option);
//            config.enableReserveRaw(enableRawFile);
        if (compress)
        takePhoto.onEnableCompress(config,showProgressBar);
        else
        takePhoto.onEnableCompress(null,false);//不压缩


    }

    /****
     * 设置剪裁 选项
     * @return
     */
    private static CropOptions getCropOptions(){
        int height= 580;
        int width= 856;
        boolean withWonCrop=true;//是否使用自带 裁剪工具
        CropOptions.Builder builder=new CropOptions.Builder();
        builder.setAspectX(width).setAspectY(height);// 尺寸/比例  宽/高 二选一
       // builder.setOutputX(width).setOutputY(height);// 尺寸/比例  宽X高
        builder.setWithOwnCrop(withWonCrop);
        return builder.create();
    }

}
